package team.yidong.fenda.utils;

import org.springframework.stereotype.Component;
import team.yidong.fenda.payUtil.ClientCustomSSL;
import team.yidong.fenda.payUtil.PayUtil;
import team.yidong.fenda.payUtil.StringUtil;
import team.yidong.fenda.payUtil.WxPayConfig;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

/**
 * Created by xuanlong on 2018/1/11.
 */
@Component
public class MasterScoreTiXian {
    public void tixian(Double sum, String openid) {
        Map packageParams = new HashMap();
        //生成的随机字符串
        String nonce_str = StringUtil.getRandomStringByLength(32);// 随机字符串
        //商户订单号
        String out_trade_no = UUID.randomUUID().toString().replace("-", "");
        packageParams.put("mch_appid", WxPayConfig.appid);
        packageParams.put("mchid", WxPayConfig.mch_id);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("partner_trade_no", out_trade_no);
        packageParams.put("openid", openid);
        packageParams.put("check_name", "FORCE_CHECK");
        packageParams.put("amount", sum.intValue());
        packageParams.put("desc", "专家积分提现");
        packageParams.put("spbill_create_ip", "192.168.49.111");
        Iterator iter = packageParams.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object value = entry.getValue();

        }
        String preStr = PayUtil.createLinkString(packageParams);// 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
        //MD5运算生成签名，这里是第一次签名，用于调用接口
        String mysign = PayUtil.sign(preStr, WxPayConfig.key, "utf-8").toUpperCase();
        //拼接统一下单接口使用的xml数据，要将上一步生成的签名一起拼接进去
        String xml = "<xml>" + "<mch_appid>" + WxPayConfig.appid + "</mch_appid>"
                + "<mchid>" + WxPayConfig.mch_id + "</mchid>"
                + "<nonce_str>" + nonce_str + "</nonce_str>"
                + "<partner_trade_no>" + out_trade_no + "</partner_trade_no>"
                + "<openid>" + openid+ "</openid>"
                + "<check_name>FORCE_CHECK</check_name>"
                + "<amount>" +sum.intValue() + "</amount>"
                + "<desc>专家积分提现</desc>"
                + "<spbill_create_ip>192.168.49.111</spbill_create_ip>"
                + "<sign>" + mysign + "</sign>"
                + "</xml>";
        String createOrderURL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
        try {
            String s = ClientCustomSSL.doRefund(createOrderURL, xml);
            System.out.println("s:" + s);



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
